﻿using AgvDispatchSystemWpf.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AgvDispatchSystemWpf.Model
{
    public class MonitorValueModel: NotifyPropertyBase 
    {
        public Action<MonitorValueState, string, string> ValueStateChanged;

        public string ValueId { get; set; }
        public string ValueName { get; set; }
        public string Address { get; set; }
        public string DataType { get; set; }
        public bool IsAlarm { get; set; }

        public bool AlarmPolarity { get; set; } = true;
        public double LoLoAlarm { get; set; }
        public double LowAlarm { get; set; }
        public double HighAlarm { get; set; }
        public double HiHiAlarm { get; set; }

        public string Unit { get; set; }

        private Storage _currentValue;

        public Storage CurrentValue
        {
            get { return _currentValue; }
            set
            {
                _currentValue = value;
                if (IsAlarm)
                {
                    string msg = ValueDesc;
                    MonitorValueState state = MonitorValueState.OK;

                    if (DataType=="Float")
                    {
                        if (value.Single < LoLoAlarm)
                        { msg += "极低"; state = MonitorValueState.LoLo; }
                        else if (value.Single < LowAlarm)
                        { msg += "过低"; state = MonitorValueState.Low; }
                        else if (value.Single > HiHiAlarm)
                        { msg += "极高"; state = MonitorValueState.HiHi; }
                        else if (value.Single > HighAlarm)
                        { msg += "过高"; state = MonitorValueState.High; }
                    }
                    else if (DataType=="Bool")
                    {
                        if (AlarmPolarity)
                        {
                            if (value.Boolean)
                            {
                                msg += "On"; state = MonitorValueState.OnAlarm;
                            }                           
                        }
                        else
                        {
                            if (!value.Boolean)
                            {
                                msg += "Off"; state = MonitorValueState.OffAlarm;
                            }
                        }
                    }                    
                    ValueStateChanged(state, msg + "。当前值：" + value.ToString(), ValueId);
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 变量表述
        /// </summary>
        public string ValueDesc { get; set; }
    }
}
